<!DOCTYPE html>
<html class="x-admin-sm">

    <head>
        <meta charset="UTF-8">
        <title>供应商-编辑</title>
        <meta name="renderer" content="webkit">
        <link rel="stylesheet" href="../../css/font.css"/>
        <link rel="stylesheet" href="../../css/xadmin.css"/>
        <script src="../../lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../js/xadmin.js"></script>
        <script src="../../js/vue2.7min.js"></script>
        <script src="../../js/axios.min.js"></script>
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form" id="app">


                    <div class="layui-form-item" v-if="id===1">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>供应商所属管理员的id</label>
                        <div class="layui-input-inline">
                            <input type="text"
                                   class="layui-input"
                                   v-model="supplierManagerId"></div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>供应商名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="L_username" required="" lay-verify="name" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierName"></div>
                        <div class="layui-form-mid layui-word-aux">2到20个字符</div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>手机号码</label>
                        <div class="layui-input-inline">
                            <input type="text" required="" lay-verify="realName" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierPhone"></div>
                        <div class="layui-form-mid layui-word-aux">11位电话</div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>地址</label>
                        <div class="layui-input-inline">
                            <input type="text" required="" lay-verify="realName" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierAddress"></div>
                        <div class="layui-form-mid layui-word-aux">地址信息</div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>信息</label>
                        <div class="layui-input-inline">
                            <input type="text" required="" lay-verify="realName" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierMessage"></div>
                        <div class="layui-form-mid layui-word-aux">备注信息</div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>邮箱</label>
                        <div class="layui-input-inline">
                            <input type="text" required="" lay-verify="realName" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierEmail"></div>
                        <div class="layui-form-mid layui-word-aux">邮箱信息</div>
                    </div>


                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>状态</label>
                        <div class="layui-input-inline">
                            <input type="text" required="" lay-verify="status" autocomplete="off"
                                   class="layui-input"
                                   v-model="supplierStatus"></div>
                        <div class="layui-form-mid layui-word-aux">启用 1/禁用 0</div>
                    </div>


                </form>
            </div>
        </div>
        <div class="layui-form-item" id="app1">
            <label class="layui-form-label"></label>
            <button class="layui-btn" lay-submit="" @click="change">修改</button>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label"></label>
            <button class="layui-btn" lay-submit="" onClick="javascript :history.back(-1);">返回上一页</button>
        </div>





        <script type="text/javascript">
            var vm = new Vue({
                el: "#app",
                data: {
                    //管理员信息
                    id: null,
                    name: null,
                    level: null,
                    password: null,
                    phone: null,
                    realName: null,
                    sex: null,
                    status: null,
                    createTime: null,
                    updateTime: null,
                    createUser: null,
                    updateUser: null,
                    //分页属性
                    supplierId: null,
                    supplierManagerId: null,
                    supplierName: null,
                    supplierPhone: null,
                    supplierAddress: null,
                    supplierMessage: null,
                    supplierEmail: null,
                    supplierStatus: null,
                    supplierCreateTime: null,
                    supplierUpdateTime: null,
                    supplierCreateUser: null,
                    supplierUpdateUser: null

                }
            });
            var vm1 = new Vue({
                el: "#app1",
                methods: {
                    change() {
                        //首先校验数据合法性
                        if (vm.supplierName === null || vm.supplierName === "" || vm.supplierName.length < 2 || vm.supplierName.length > 200) {
                            layer.alert("供应商名不可为空且长度在[2-200]哦", {
                                icon: 6
                            })
                        } else if (vm.supplierPhone === null || vm.supplierPhone === "" || vm.supplierPhone.length !== 11) {
                            layer.alert("电话11位哦", {
                                icon: 6
                            })
                        } else if (vm.supplierAddress === null || vm.supplierAddress === "" || vm.supplierAddress.length < 2 || vm.supplierAddress.length > 200) {
                            layer.alert("地址不可为空且长度在[2-200]哦", {
                                icon: 6
                            })
                        } else if (vm.supplierMessage === null || vm.supplierMessage === "" || vm.supplierMessage.length < 2 || vm.supplierMessage.length > 200) {
                            layer.alert("信息不可为空且长度在[2-200]哦", {
                                icon: 6
                            })
                        } else if (vm.supplierEmail === null || vm.supplierEmail === "" || vm.supplierEmail.length < 2 || vm.supplierEmail.length > 100) {
                            layer.alert("邮箱不可为空且长度在[2-100]哦", {
                                icon: 6
                            })
                        } else if (vm.supplierStatus === null || vm.supplierStatus === "" || (vm.supplierStatus !== "1" && vm.supplierStatus !== "0")) {
                            layer.alert("状态只可以1/0哦", {
                                icon: 6
                            })
                        } else {
                            console.log("开始上传数据");                      //开始axios通信
                            var jsonData = {
                                managerId: vm.supplierManagerId,
                                name: vm.supplierName,
                                phone: vm.supplierPhone,
                                address: vm.supplierAddress,
                                message: vm.supplierMessage,
                                email: vm.supplierEmail,
                                status: vm.supplierStatus,
                                id: vm.supplierId
                            }
                            axios.post("../../../supplier/editById", jsonData)
                                .then(function (response) {
                                    //数据保存到vue全局
                                    console.log(response)
                                    if (response.data.code === 1) {
                                        layer.alert("数据编辑成功", {
                                            icon: 6
                                        })
                                    }
                                }, function (err) {
                                    console.log(err);

                                })
                        }


                    },
                    //获取url参数
                    url1(name) {
                        //获取上一地址中的get参数
                        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                        var r = window.location.search.substr(1).match(reg);
                        if (r != null)
                            return unescape(r[2]);
                        return null;
                    },
                    getOneById(id) {
                        //修改管理员状态
                        var jsonData = {id: id}
                        axios.post("../../../supplier/getOneById", jsonData)
                            .then(function (response) {
                                //数据保存到vue全局
                                if (response.data.code === 1) {
                                    vm.supplierId = response.data.data.id;
                                    vm.supplierManagerId = response.data.data.managerId;
                                    vm.supplierName = response.data.data.name;
                                    vm.supplierPhone = response.data.data.phone;
                                    vm.supplierAddress = response.data.data.address;
                                    vm.supplierEmail = response.data.data.email;
                                    vm.supplierMessage = response.data.data.message;
                                }
                            }, function (err) {
                                console.log(err);

                            })
                    },
                    //根据id获取账号信息。判断身份，展示不同页面信息
                    getManagerInfo() {
                        axios.post("../../../manager/getOneById")
                            .then(function (response) {
                                //数据保存到vue全局
                                if (response.data.code === 1) {
                                    vm.id = response.data.data.id;
                                    vm.name = response.data.data.name;
                                    vm.level = response.data.data.level;
                                    vm.phone = response.data.data.phone;
                                    vm.sex = response.data.data.sex;
                                    vm.status = response.data.data.status;
                                }
                            }, function (err) {
                                console.log(err);

                            })
                    },
                },
                mounted: function () {
                    //从url获取被操作的id
                    var aimId = this.url1("id");
                    //查询后台
                    this.getOneById(aimId);
                    //判断操作者身份，展示修改管理员的选项
                    this.getManagerInfo();

                }
            })
        </script>
    </body>

</html>